<template>
  <div class="login">
    <el-card class="box-card">
      <div slot="header">
        <span>NetPostStation后台管理</span>
      </div>
      <el-form label-width="80px" :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>

      </el-form>
    </el-card>

  </div>
</template>

<script>

import {login} from "@/api/user";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          login(this.loginForm).then(res => {
            localStorage.setItem('token', res.data)
            localStorage.setItem('username', this.loginForm.username)
            this.$message({message: '登录成功', type: 'success'})
            this.$router.push('/home')
          });
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  background: #409eff;

  .box-card {
    width: 450px;
    margin: 200px auto;

    .el-card__header {
      font-size: 28px;
    }

    .el-button {
      width: 100%;
    }
  }
}
</style>
